/*
 * @Author: @yzcheng
 * @Date: 2021-03-13 13:00:38
 * @Version: 1.0
 * @LastEditors: @yzcheng
 * @Description: 
 * @LastEditTime: 2021-03-13 13:52:26
 */

import * as api from '@/api/flowInstance'
import { get, map } from 'lodash';
import dayjs from 'dayjs';
import { ElMessage } from 'element-plus';

export default {
  namespaced: true,
  state: () => ({
    list: [],
    loading: false,
    form: {
      instanceBusinessKey: null, //业务流程key
      date: null,
      endTime: null, // 结束时间
      stauts: null, //状态
      processName: "", // 流程名称
      processDefinitionKey: "", //流程Key
      startTime: "", // 开始时间
      involvedUser: "", // 发起人
      instanceBusinessState: null, // 流程状态
      processInstanceId: "", // 流程ID
      flowType: "", // 流程分类
    },
    pagination: {
      total: 0,
      pageSize: 10,
      current: 1,
    },
    flowStatus: []
  }),
  getter: {},
  actions: {
    async fetchListRequest({ state, commit }) {
      commit('TOGGLE_LOADING', true);
      const { pagination, form } = state;
      const [startTime, endTime] = map(form.date, (item) => {
        return item.getTime()
      });
      const username = form?.involvedUser?.split("-")[1]
      const params = {
        ...form,
        involvedUser: username,
        startTime,
        endTime,
        date: undefined,
      }
      let { data } = await api.fetchList(pagination, params);
      data = data.data
      commit('SAVE_LIST', data.records || []);
      commit('SAVE_PAGINATION', {
        pageSize: get(data, 'size', 10),
        pageNum: get(data, 'current', 1),
        total: get(data, 'total', 0),
      })
      commit('TOGGLE_LOADING', false);
    },
    async deleteRecordRequest({ commit, dispatch }, payload) {
      const res = await api.deleteRecord(payload);
      if (res.data.code === 200) {
        commit('SAVE_PAGINATION', { current: 1, pageNum: 1 })
        dispatch("fetchListRequest");
      }
    },
    async jumpFreeRequest({ commit }, payload) {
      // const { success, message } = await api.jumpFreeRequest(payload)
      let res = await api.jumpFreeRequest(payload)
      const { success, message } = res.data
      if (success) {
        ElMessage.success({
          message: message,
        })
      }
      return success;
    },

    //强制办结
    async closeFlowRequest({ dispatch }, payload) {
      // const { success, message } = await api.closeFlowRequest(payload);
      let res = await api.closeFlowRequest(payload);
      const { success, message } = res.data
      if (success) {
        dispatch("fetchListRequest");
        ElMessage.success({
          message: message,
        })
      }
    },
    //废弃流程
    async discardFlowRequest({ dispatch }, payload) {
      // const { success, message } = await api.discardFlowRequest(payload);
      let res = await api.discardFlowRequest(payload);
      const { success, message } = res.data
      if (success) {
        dispatch("fetchListRequest");
        ElMessage.success({
          message: message,
        })
      }
    },

    // 获取流程状态字典项
    async fetchFlowStatus({ commit }) {
      let { data } = await api.requestFlowStatus({ indexEnumId: "1" });
      data = data.data
      commit("SAVE_FLOW_STATUS", data);
    }
  },
  mutations: {
    TOGGLE_LOADING(state, payload) {
      state.loading = payload;
    },
    SAVE_LIST(state, payload) {
      state.list = payload;
    },
    SAVE_PAGINATION(state, payload) {
      state.pagination = {
        ...state.pagination,
        ...payload
      }
    },
    SAVE_FORM(state, payload) {
      state.form = {
        ...state.form,
        ...payload,
      }
    },
    SAVE_FLOW_STATUS(state, payload) {
      state.flowStatus = payload;
    }
  },
}
